<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="货物id" prop="goodsId">
        <el-input
          v-model="queryParams.goodsId"
          placeholder="请输入货物id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货物数量" prop="goodsNum">
        <el-input
          v-model="queryParams.goodsNum"
          placeholder="请输入货物数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="运输开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运输开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="运输结束时间" prop="endeTime">
        <el-date-picker clearable
          v-model="queryParams.endeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择运输结束时间">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:logistics:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:logistics:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:logistics:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:logistics:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logisticsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="出发地" align="center" prop="startAddress" />
      <el-table-column label="目的地" align="center" prop="endAddress" />
      <el-table-column label="收货方" align="center" prop="storeName" />
      <el-table-column label="货物名称" align="center" prop="goodsName" />
      <el-table-column label="货物数量" align="center" prop="goodsNum" />
      <el-table-column label="运输方式" align="center" prop="transportType" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.transportType==0">陆运</el-tag>
          <el-tag v-else-if="scope.row.transportType==1">海运</el-tag>
          <el-tag v-else-if="scope.row.transportType==2">空运</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="transportType" >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.isFault==0">未运输</el-tag>
          <el-tag v-else-if="scope.row.isFault==1">运输中</el-tag>
          <el-tag v-else-if="scope.row.isFault==2">已送达</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="运输开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运输结束时间" align="center" prop="endeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计结束时间" align="center" prop="predictTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="startTransport(scope.row)"
            v-if="scope.row.isFault==0"
          >开始运输</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="delivery(scope.row)"
            v-if="scope.row.isFault==1"
          >送达</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物流记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="出发地" prop="startAddress">
          <el-input v-model="form.startAddress" placeholder="请输入出发地" />
        </el-form-item>
        <el-form-item label="目的地" prop="endAddress">
          <el-input v-model="form.endAddress" placeholder="请输入目的地" />
        </el-form-item>
        <el-form-item label="发货方" prop="consigner">
          <el-input v-model="form.consigner" placeholder="请输入发货方" />
        </el-form-item>
        <el-form-item label="收货方" prop="consignee">
          <el-input v-model="form.consignee" placeholder="请输入收货方" />
        </el-form-item>
        <el-form-item label="货物id" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入货物id" />
        </el-form-item>
        <el-form-item label="货物数量" prop="goodsNum">
          <el-input v-model="form.goodsNum" placeholder="请输入货物数量" />
        </el-form-item>
        <el-form-item label="运输开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择运输开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="运输结束时间" prop="endeTime">
          <el-date-picker clearable
            v-model="form.endeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择运输结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="物流公司" prop="logisticsCompany">
          <el-input v-model="form.logisticsCompany" placeholder="请输入物流公司" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="0正常 1删除" prop="isFault">
          <el-input v-model="form.isFault" placeholder="请输入0正常 1删除" />
        </el-form-item>
        <el-form-item label="司机" prop="isFault">
          <el-select v-model="form.driverId" placeholder="请选择司机">
            <el-option
              v-for="item in driverList"
              :key="item.userId"
              :label="item.nickName"
              :value="item.userId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLogistics, getLogistics, delLogistics, addLogistics, updateLogistics,getUserByDept } from "@/api/source/logistics/logistics";

export default {
  name: "Logistics",
  dicts: ['src_material_type'],
  data() {
    return {
      driverList:{},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流记录表格数据
      logisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startAddress: null,
        endAddress: null,
        goodsId: null,
        goodsNum: null,
        transportType: null,
        startTime: undefined,
        endeTime: null,
        logisticsCompany: null,
        isFault: null,
        goodsType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    getUserByDept().then(res=>{
      console.log(res.data)
      this.driverList=res.data;
    })
  },
  methods: {
    normalizer(node) {
      return {
        id: node.dictCode,
        label: node.dictLabel,
        children: node.children
      };
    },

    delivery(row){
      row.isFault=2;
      this.$modal.confirm('是否确认送达物流记录？').then(function() {
        return updateLogistics(row);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("送达成功");
      });
    },
    startTransport(row){
      row.isFault=1;
      updateLogistics(row).then(res=>{
        this.$modal.msgSuccess("已开始运输");
        this.getList();
      })
    },
    /** 查询物流记录列表 */
    getList() {
      this.loading = true;
      listLogistics(this.queryParams).then(response => {
        this.logisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        logisticsId: null,
        startAddress: null,
        endAddress: null,
        consigner: null,
        consignee: null,
        goodsId: null,
        goodsNum: null,
        transportType: null,
        startTime: null,
        endeTime: null,
        logisticsCompany: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        isFault: null,
        goodsType: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.logisticsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const logisticsId = row.logisticsId || this.ids
      getLogistics(logisticsId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.logisticsId != null) {
            updateLogistics(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLogistics(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const logisticsIds = row.logisticsId || this.ids;
      this.$modal.confirm('是否确认删除物流记录编号为"' + logisticsIds + '"的数据项？').then(function() {
        return delLogistics(logisticsIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/logistics/export', {
        ...this.queryParams
      }, `logistics_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
